<!DOCTYPE html><html lang="[&quot;zh-Hans&quot;,&quot;zh-CN&quot;,&quot;default&quot;]"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="前端性能"><meta name="keywords" content="性能优化"><meta name="author" content="wenmu"><meta name="copyright" content="wenmu"><title>前端性能 | 温木的博客</title><link rel="shortcut icon" href="/melody-favicon.ico"><link rel="stylesheet" href="/css/index.css?version=1.6.1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.6.1"><link rel="dns-prefetch" href="https://cdn.staticfile.org"><link rel="dns-prefetch" href="https://cdn.bootcss.com"><link rel="dns-prefetch" href="https://creativecommons.org"><link rel="dns-prefetch" href="http://ta.qq.com"><script>(function() {
   var hm = document.createElement("script");
   hm.src = "https://tajs.qq.com/stats?sId=&lt;script type=&quot;text/javascript&quot; src=&quot;http://tajs.qq.com/stats?sId=66523541&quot; charset=&quot;UTF-8&quot;&gt;&lt;/script&gt;";
   var s = document.getElementsByTagName("script")[0];
   s.parentNode.insertBefore(hm, s);
 })();</script><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  }
} </script><meta name="generator" content="Hexo 4.2.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#请求过程中潜在的一些性能优化点"><span class="toc-number">1.</span> <span class="toc-text">请求过程中潜在的一些性能优化点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#资源压合并与压缩"><span class="toc-number">2.</span> <span class="toc-text">资源压合并与压缩</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#具体需要处理的方面"><span class="toc-number">2.1.</span> <span class="toc-text">具体需要处理的方面</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#html-压缩"><span class="toc-number">2.1.1.</span> <span class="toc-text">html 压缩</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#如何进行-html-压缩"><span class="toc-number">2.2.</span> <span class="toc-text">如何进行 html 压缩</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#css-压缩"><span class="toc-number">2.3.</span> <span class="toc-text">css 压缩</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#js-压缩与混乱"><span class="toc-number">2.4.</span> <span class="toc-text">js 压缩与混乱</span></a></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="https://i.loli.net/2020/02/27/xwaBoOj158MfNyq.jpg"></div><div class="author-info__name text-center">wenmu</div><div class="author-info__description text-center">记录一些在工作中或学习新知识时学到的一些知识点；正所谓好记性不如一个烂笔头，温故而知新！</div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">115</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">52</span></a><a class="author-info-articles__categories article-meta" href="/categories"><span class="pull-left">分类</span><span class="pull-right">30</span></a></div></div></div><div id="content-outer"><div class="no-bg" id="top-container"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">温木的博客</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus"><a class="site-page" href="/">首页</a><a class="site-page" href="/archives">文章</a><a class="site-page" href="/tags">标签</a><a class="site-page" href="/categories">分类</a></span></div><div id="post-info"><div id="post-title">前端性能</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2020-01-08</time><span class="post-meta__separator">|</span><i class="fa fa-inbox post-meta__icon" aria-hidden="true"></i><a class="post-meta__categories" href="/categories/JavaScript/">JavaScript</a></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><p>总结一些前端性能的笔记。</p>
<a id="more"></a>

<h2 id="请求过程中潜在的一些性能优化点"><a href="#请求过程中潜在的一些性能优化点" class="headerlink" title="请求过程中潜在的一些性能优化点"></a>请求过程中潜在的一些性能优化点</h2><ol>
<li>dns 是否可以通过缓存减少 dns 查询时间？</li>
<li>网络请求的过程走最近的网络环境？</li>
<li>相同的静态资源是否可以缓存？</li>
<li>能否减小 http 请求的大小？</li>
<li>减少 http 请求数量</li>
<li>服务端渲染</li>
</ol>
<h2 id="资源压合并与压缩"><a href="#资源压合并与压缩" class="headerlink" title="资源压合并与压缩"></a>资源压合并与压缩</h2><p>这涉及到两方面的内容</p>
<ol>
<li>减少 http 请求数量</li>
<li>减小请求资源的大小</li>
</ol>
<h3 id="具体需要处理的方面"><a href="#具体需要处理的方面" class="headerlink" title="具体需要处理的方面"></a>具体需要处理的方面</h3><ul>
<li>html 压缩</li>
<li>css 压缩</li>
<li>js 的压缩和混乱</li>
<li>文件合并</li>
<li>开启 gzip</li>
</ul>
<h4 id="html-压缩"><a href="#html-压缩" class="headerlink" title="html 压缩"></a>html 压缩</h4><p>HTML 代码压缩就是压缩这些在文本文件中有意义，但是在 HTML 中不显示的字符，包括空格、制表符、换行符等，还有一些其他意义的字符，比如 HTML 注释也可以被压缩。<br>这些在我们写代码时有帮助，但对浏览器的解析没有意义，因此都可以被省略掉。</p>
<ul>
<li>蓝色是没压缩的 HTML</li>
<li>红色是进行了 HTML 压缩</li>
<li>绿色是进行了 gzip 压缩<br>仅仅进行 HTML 压缩，貌似压缩效果不是太明显，其实不然，比如谷歌这样的公司，他的流量站整个互联网的 40%，如果 1M 减少一个字节，每年就可以节省流量近 500TB，相当于上亿元的支出，当然如果网站的流量很小，那就不再讨论范围内了。</li>
</ul>
<h3 id="如何进行-html-压缩"><a href="#如何进行-html-压缩" class="headerlink" title="如何进行 html 压缩"></a>如何进行 html 压缩</h3><ol>
<li>在线的网站进行压缩<br>但是现在前端开发都会使用构建工具，所以这个一般用不上。</li>
<li><code>nodejs</code>提供的 <code>html-minifier</code> 工具</li>
<li>后端模板引擎渲染压缩</li>
</ol>
<h3 id="css-压缩"><a href="#css-压缩" class="headerlink" title="css 压缩"></a>css 压缩</h3><ul>
<li>无效代码删除</li>
<li>css 语义合并<br>一些语义重复的代码进行合并<br>如何进行 css 压缩</li>
</ul>
<ol>
<li>使用在线网站进行压缩</li>
<li>使用 html-minifier 对 html 中的 css 进行压缩</li>
<li>使用 clean-css 对 css 进行压缩</li>
</ol>
<h3 id="js-压缩与混乱"><a href="#js-压缩与混乱" class="headerlink" title="js 压缩与混乱"></a>js 压缩与混乱</h3><ol>
<li>无效的字符删除</li>
<li>剔除注释</li>
<li>代码语义的缩进和优化<br>比如变量、方法名替换成单字符等等</li>
<li>代码保护</li>
</ol>
<p>如何进行 js 压缩</p>
<ol>
<li>使用在线网站进行压缩</li>
<li>使用 html-minifier 对 html 中的 css 进行压缩</li>
<li>使用 unlifyjs2 对 js 进行压缩</li>
</ol>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">wenmu</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://blog.wangpengpeng.site/2020/01/08/%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD/">http://blog.wangpengpeng.site/2020/01/08/%E5%89%8D%E7%AB%AF%E6%80%A7%E8%83%BD/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://blog.wangpengpeng.site">温木的博客</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">性能优化</a></div><div class="post-qr-code"><div class="post-qr-code-item"><img class="post-qr-code__img" src="https://i.loli.net/2020/02/27/hOBzIxFDJMCrmtN.jpg"><div class="post-qr-code__desc">微信打赏</div></div></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2020/01/08/Electron%E5%85%A5%E9%97%A8%E7%AC%94%E8%AE%B0/"><i class="fa fa-chevron-left">  </i><span>Electron入门笔记</span></a></div><div class="next-post pull-right"><a href="/2020/01/08/%E5%BC%80%E5%8F%91%E5%B9%B6%E5%8F%91%E5%B8%83%E5%92%8C%E6%9B%B4%E6%96%B0%E4%B8%80%E4%B8%AAnpm%E5%8C%85/"><span>开发并发布和更新一个npm包</span><i class="fa fa-chevron-right"></i></a></div></nav><div id="vcomment"></div><script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script><script>var notify = 'false' == true ? true : false;
var verify = 'false' == true ? true : false;
var GUEST_INFO = ['nick','mail','link'];
var guest_info = 'nick,mail,link'.split(',').filter(function(item){
  return GUEST_INFO.indexOf(item) > -1
});
guest_info = guest_info.length == 0 ? GUEST_INFO :guest_info;
window.valine = new Valine({
  el:'#vcomment',
  notify:notify,
  verify:verify,
  appId:'U1QqpeQNuBnRsfygHk1GQ30q-gzGzoHsz',
  appKey:'dunDQzEseDzgsKgw4qLQcog1',
  placeholder:'支持markdown！',
  avatar:'wavatar',
  guest_info:guest_info,
  pageSize:'20',
  lang: 'zh-cn'
})</script></div></div><footer><div class="layout" id="footer"><div class="copyright">&copy;2019 - 2020 By wenmu</div><div class="framework-info"><span>驱动 - </span><a href="http://hexo.io" target="_blank" rel="noopener"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a href="https://github.com/Molunerfinn/hexo-theme-melody" target="_blank" rel="noopener"><span>Melody</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv">该文访问量<span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.6.1"></script><script src="/js/fancybox.js?version=1.6.1"></script><script src="/js/sidebar.js?version=1.6.1"></script><script src="/js/copy.js?version=1.6.1"></script><script src="/js/fireworks.js?version=1.6.1"></script><script src="/js/transition.js?version=1.6.1"></script><script src="/js/scroll.js?version=1.6.1"></script><script src="/js/head.js?version=1.6.1"></script><script>if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
}</script></body></html>